
﻿using Microsoft.AspNetCore.Server.IISIntegration;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Server.BasicsDatumServer.LocationServer.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SmartWMS.Server.BasicsDatumServer.LocationServer
{
    public class LocationServers : ILocationServers
    {


        private readonly IRepository<Location> _location;

        private readonly IRepository<Warehouse> _warehouse;

        public LocationServers(IRepository<Location> location, IRepository<Warehouse> warehouse)
        {
            _location = location;
            _warehouse = warehouse;
        }
        /// <summary>
        /// 仓库下拉
        /// </summary>
        /// <returns></returns>
        public async Task<List<Warehouse>> GetWarehousBoxAsync() {
            try
            {
                var list=await _warehouse.GetAllListAsync();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 库位管理列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public  Response<List<LocationDtoPes>> GetLocationDtoPes(PaginationDto dto) {
            try
            {
                var llist=  _location.GetListQuery();
                var wlist=  _warehouse.GetListQuery();
                var list=(from a in llist
                          join b in wlist on a.WarehouseId equals b.Id
                          select new LocationDtoPes {
                            Id=a.Id,
                            WarehouseId=b.Id,
                              Name=b.Name,
                            LocationCode =a.LocationCode,
                            LocationName=a.LocationName,
                              LocationType=a.LocationType,
                              Locationarea=a.Locationarea,
                              CreateTime=a.CreateTime,
                              Remarks=a.Remarks,
                              IsDefault=a.IsDefault,
                              IsDisable=a.IsDisable,
                          }).ToList();
                if (!string.IsNullOrEmpty(dto.LocationCode)) {
                    list = list.Where(t => t.LocationCode.Contains(dto.LocationCode)).ToList();
                }
                if (!string.IsNullOrEmpty(dto.LocationName))
                {
                    list = list.Where(t => t.LocationName.Contains(dto.LocationName)).ToList();
                }
                if (dto.WarehouseId!=0)
                {
                    list = list.Where(t => t.WarehouseId==dto.WarehouseId).ToList();
                }
                int totalCount= list.Count();
                int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.size);
                list = list.Skip((dto.page - 1) * dto.size).Take(dto.size).ToList();
                //把获取到的数据传进ok();
                return list != null ? Response<List<LocationDtoPes>>.Ok(list, totalCount, pageCount) : Response<List<LocationDtoPes>>.Fail(new List<LocationDtoPes>());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加库位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddLocationAsync(LocationDtoPes dto)
        {
            try
            {
                if (dto.IsDefault)
                {
                    var defaultLocation = _location.GetListQuery().FirstOrDefault(t => t.IsDefault == true);
                    if (defaultLocation != null)
                    {
                        defaultLocation.IsDefault = false;
                        await _location.UpdateAsync(defaultLocation);
                    }
                }

                var result = await _location.InsertAsync(dto.MapTo<Location>());

                if (result > 0)
                {
                    return Response<bool>.Ok(true);
                }
                else
                {
                    return Response<bool>.Fail(false);
                }
                ////默认库位只能有一个
                //if (dto.IsDefault == true)
                //{
                //    var list = _location.GetListQuery().Where(t => t.IsDefault == true);
                //    foreach (var item in list)
                //    {
                //        var plist=_location.GetListQuery().FirstOrDefault(t => t.Id == item.Id);
                //        plist.IsDefault = false;
                //        await _location.UpdateAsync(plist);
                //    }
                //}
                //else { 

                //}
                //var resault = await _location.InsertAsync(dto.MapTo<Location>());
                //return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填库位1
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public Response<LocationDtoPes> RackLocation(LocationDtoPes dto) {
            try
            {
                var list = _location.GetListQuery().FirstOrDefault(x => x.Id == dto.Id);
                var result= list.MapTo<LocationDtoPes>();
                return Response<LocationDtoPes>.Ok(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改库位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateLocationAsync(LocationDtoPes dto)
        {
            try
            {
                if (dto.IsDefault)
                {
                    var defaultLocation = _location.GetListQuery().FirstOrDefault(t => t.IsDefault == true);
                    if (defaultLocation != null)
                    {
                        defaultLocation.IsDefault = false;
                        await _location.UpdateAsync(defaultLocation);
                    }
                }
                var resault = await _location.UpdateAsync(dto.MapTo<Location>());
                return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改库位状态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public  async Task<Response<bool>> UpdateLocationIsDisable(LocationDtoPes dto) {
            try
            {
               //删除当前列表
               var list=_location.GetListQuery().FirstOrDefault(t => t.Id == dto.Id);
                if (list != null)
                {
                    list.IsDisable = !list.IsDisable;
                }
                var resault = await _location.UpdateAsync(list);
                return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 库位删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteLocationIsDisable(LocationDtoPes dto)
        {
            try
            {
                //删除当前列表
                var list = _location.GetListQuery().FirstOrDefault(t => t.Id == dto.Id);
                var result = await _location.DeleteByIdAsync(list);
                return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除库位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> DeletesLocationIsDisable(LocationDtoPes dto)
        {
            try
            {
                var sid=dto.DelId.Split(',');

                var list=new List<Location>();  
                foreach (var item in sid)
                {
                    var plist= _location.GetListQuery().Where(t => t.Id == int.Parse(item)).ToList();
                    list.AddRange(plist);
                }
                var result = _location.DeleteAsync(list);
                return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));

            }
            catch (Exception)
            {

                throw;
            }
        }



    }
}
